package com.learning.task3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;

/**
 * 
 * @author AnND1
 * 
 */
public class MyHandler {
	private ArrayList<Person> personList;
	private final int NUM_PERSON = 4;// value need is 50 but use 4 to test
	private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static final int NAME_LENGTH = 10;
	private static final int ADRESS_LENGTH = 15;
	private static final int MAX_AGE = 80;
	private static final int MIN_AGE = 15;

	public ArrayList<Person> getPersonList() {
		return personList;
	}

	public void setPersonList(ArrayList<Person> personList) {
		this.personList = personList;
	}

	/**
	 * Adding person at positon index using listIterator
	 * 
	 * Report: when we use listIterater to add object the Arraylist will be
	 * added by this object
	 */
	public void addPerson() {
		System.out.println("add person");
		ListIterator<Person> iterator = personList.listIterator();
		while (iterator.hasNext()) {
			Person person=iterator.next();	
			iterator.add(person);
			System.out.println("running------------------------------");
				
		}
		displayPerson();
		// 
	}

	/**
	 * initial Person
	 * 
	 * @return
	 */

	public Person generatePerson() {
		Person person = new Person();
		int age = randomAge(MIN_AGE, MAX_AGE);
		String name = randomName(NAME_LENGTH);
		String adress = randomName(ADRESS_LENGTH);
		person.setAge(age);
		person.setAdress(adress);
		person.setName(name);
		return person;
	}

	/**
	 * Generate person list
	 */
	public void generatePersonList() {
		personList = new ArrayList<Person>();
		for (int i = 0; i < NUM_PERSON; i++) {
			Person e = generatePerson();
			personList.add(e);
		}
	}

	/**
	 * print person list using iterator
	 */
	public void displayPerson() {
		Iterator<Person> iterator = personList.iterator();
		while (iterator.hasNext()) {
			Person myPerson = (Person) iterator.next();
			System.out.println("Name:" + myPerson.getName());
			System.out.println("Adress:" + myPerson.getAdress());
			System.out.println("Age :" + myPerson.getAge());
			System.out.println("---------------");

		}

	}

	/**
	 * print person list using IteratorList
	 */

	public void displayPerson1() {
		ListIterator<Person> listIterator = personList.listIterator();
		while (listIterator.hasNext()) {
			Person myPerson = (Person) listIterator.next();
			System.out.println("Name:" + myPerson.getName());
			System.out.println("Adress:" + myPerson.getAdress());
			System.out.println("Age :" + myPerson.getAge());
			System.out.println("---------------");
		}
	}

	/**
	 * print person list using IteratorList from lastitem to firstitem
	 */

	public void displayPerson2() {
		ListIterator<Person> listIterator = personList.listIterator(personList
				.size());
		while (listIterator.hasNext()) {
			Person myPerson = (Person) listIterator.next();
			System.out.println("Name:" + myPerson.getName());
			System.out.println("Adress:" + myPerson.getAdress());
			System.out.println("Age :" + myPerson.getAge());
			System.out.println("---------------");
		}
	}

	/**
	 * print person with name sorted DESC
	 */

	public void displayPerson3() {
		System.out.println("fisrt:");
		displayPerson();
		Comparator<Person> comparator = new Comparator<Person>() {

			@Override
			public int compare(Person p1, Person p2) {
				if (p1.getName().compareTo(p2.getName()) > 0) {
					return -1;
				} else {
					return 1;
				}
			}
		};
		List<Person> persons = getPersonList();
		Collections.sort(persons, comparator);
		System.out.println("after:");
		displayPerson();
	}

	/**
	 * use to display using foreach
	 */
	public void displayUsingForEach() {
		for (Person person : personList) {
			System.out.println(person.getName());
			System.out.println(person.getAdress());
			System.out.println(person.getAge());

		}
	}

	/**
	 * create random string
	 * 
	 * @param length
	 * @return
	 */
	public String randomName(int length) {
		StringBuffer randStr = new StringBuffer();
		for (int i = 0; i < length; i++) {
			Random randomGenerator = new Random();
			int randomInt = randomGenerator.nextInt(CHAR_LIST.length());
			char ch = CHAR_LIST.charAt(randomInt);
			randStr.append(ch);
		}
		return randStr.toString();
	}

	/**
	 * Create random age
	 * 
	 * @param aStart
	 * @param aEnd
	 * @return
	 */
	private static int randomAge(int aStart, int aEnd) {
		Random random = new Random();
		long range = (long) aEnd - (long) aStart + 1;
		// compute a fraction of the range, 0 <= frac < range
		long fraction = (long) (range * random.nextDouble());
		int randomNumber = (int) (fraction + aStart);
		return randomNumber;
	}

}
